<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//按收入分担
class ShareIncome extends CommonController {

	function __construct(){
		
		parent::__construct();
		
		$this->load->model('Accounting/accountingModel','CurModel');
	}
	
	
	//start
	/**
	* @name 获取数据
	* @param xdata ture char 横向
	* @param ydata ture char 纵向
	* @param f ture char 筛选
	* @return 成功返回1
	*/
	//end
	function get_data(){
		
		if(!$this->check_priv('accounting_income|accounting_income_1')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$xdata = $this->input->post('xdata',null,array());
		$ydata = $this->input->post('ydata',null,array());
		$filter = $this->input->post('f',null,array());
		
		if(empty($filter['date_month'])){
			$this->err_out('请选择月份');
		}

		
		$xlist = $this->get_xlist($xdata,$ydata);
		
		if(!isset($ydata['list'])){
			$this->err_out('请选择纵向数据');
		}
		$ylist = array();
		$ls_list = $ydata['list'];
		
		$bs = !empty($ydata['info']['exchange_rate'])?$ydata['info']['exchange_rate']:1;
		$total = 0;
		foreach($ls_list as $key=>$val){
			$ls_data = array();
			$ls_data['name'] = $val;
			
			$arr = array(
				'date_month'=>$filter['date_month'],
				'subject'=>$val
			);
			$ls_total = $this->CurModel->get_totals($arr);
			$ls_data['total'] = $ls_total * $bs;
			$total += $ls_data['total'];
			/*
			foreach($xlist as $k=>$v){
				if($k<2){
					continue;
				}
				if(!isset($v['children'])){
					$arr = array(
						'date_month'=>$filter['date_month'],
						'subject'=>$val,
						'bumen'=>$v['value']
					);
					//$ls_total =  $this->CurModel->get_totals($arr);
					//$ls_data[$v['key']] = $ls_total * $bs;
				}
				else{
					foreach($v['children'] as $item){
						$arr = array(
							'date_month'=>$filter['date_month'],
							'subject'=>$val,
							'bumen'=>$v['value'],
							'region'=>$item['value']
						);
						//$ls_total =  $this->CurModel->get_totals($arr);
						//$ls_data[$item['key']] = $ls_total * $bs;
					}
				}
				
			}
			*/
			
			$ylist[] = $ls_data;
		}
		$ls_data = array(
			'name'=>'分摊费用',
			'total'=>$total
		);
		$ylist[] = $ls_data;
		
		$data = array(
			'xlist'=>$xlist,
			'ylist'=>$ylist
		);
		
		
		$this->data_out($data);
	}
	
	function get_xlist($xdata,$ydata){
		$xlist = array();
		$name = '表头';
		// $name = !empty($ydata['info']['name'])?$ydata['info']['name']:'表头';
		// if(!empty($ydata['info']['bz'])){
		// 	$name .="({$ydata['info']['bz']})";
		// }
		
		
		$xlist[] = array('key'=>'name','value'=>$name);
		$xlist[] = array('key'=>'total','value'=>'总金额');
			
		foreach($xdata as $key=>$val){
			if(empty($val['name'])){
				continue;
			}
			$ls_data = array();
			$ls_data['key'] = 'bumen'.$key;
			$ls_data['value'] = $val['name'];
			$ls_data['classRule'] = $val['classRule'];
			
			
			if(isset($val['data'])){
				$ls_children = array();
				
				foreach($val['data'] as $k=>$v){
					$ls_children[] = array(
						'key'=>'children'.$k,
						'value'=>$v
					);
				}
				$ls_data['children'] = $ls_children;
			}

			$xlist[] = $ls_data;
		}
		return $xlist;
	}

	
	//导出人员分摊
	function export_data(){
		
		if(!$this->check_priv('accounting_income|accounting_income_1')){
			$this->err_out(LANG_NOPRIV);
		}
		
		
		$xdata = $this->input->post('xdata',null,array());
		$ydata = $this->input->post('ydata',null,array());
		$filter = $this->input->post('f',null,array());
		$exps = $this->input->post('exps',null,array());
		
		if(empty($filter['date_month'])){
			$this->err_out('请选择月份');
		}
		
		
		
		$xlist = array();
		
		$name = !empty($ydata['info']['name'])?$ydata['info']['name']:'表头';
		if(!empty($ydata['info']['bz'])){
			$name .="({$ydata['info']['bz']})";
		}

		
		$index = 0;
		foreach($exps as $val){
			$index++;
		}
		if($index<=0){
			//$this->err_out('请先搜索出数据，然后设置指数后再操作导出');
		}
		
		$xlist = $this->get_xlist($xdata,$ydata);
		
		$merges = array();
		$arr = array();
		
		$ls_index = 0;
		foreach($xlist as $key=>$val){
			$col = $this->get_char($key,'A');
			
			$arr[$col.'2'] = $val['value'];
			if(isset($val['children'])){
				
				foreach($val['children'] as $k=>$v){
					$ls_col = $this->get_char($k,$col);
					$arr[$ls_col.'3'] = $v['value'];
					
					$ls_exp = isset($exps[$v['key']])?$exps[$v['key']]:0;
					$arr[$ls_col.'4'] = $ls_exp;
					$ls_index++;
				}
				
				
				$start = $col.'2';
				$ls_col = $this->get_char(count($val['children'])-1,$col);
				$end = $ls_col.'2';
				$merges[] = $start.':'.$end;
			}
			else{
				$ls_exp = isset($exps[$val['key']])?$exps[$val['key']]:0;
				$arr[$col.'4'] = $ls_exp;
				$ls_index++;
			}
		}
		
		$arr['A1'] = date('Y年m月',strtotime($filter['date_month'].'-01'));
		$ls_col = $this->get_char($ls_index-1,'A');;
		$merges[] = "A1:{$ls_col}1";
		
		/*
		$exp_total = 0;
		foreach($exps as $val){
			$exp_total += $val;
		}
		*/
		
		$line = 4;
		$arr['A'.$line] = '人员统计';
		$ls_col = $this->get_char($ls_index-1,'A');;
		$arr['B'.$line] = "=SUM(C{$line}:{$ls_col}{$line})";
		
		if(!isset($ydata['list'])){
			$this->err_out('请选择纵向数据');
		}
		
		$ylist = array();
		$ls_list = $ydata['list'];
		
		$bs = !empty($ydata['info']['exchange_rate'])?$ydata['info']['exchange_rate']:1;
		$total = 0;
		$line = 5;
		
		foreach($ls_list as $key=>$val){
			
			$arr['A'.$line] = $val;
			
			$ls_arr = array(
				'date_month'=>$filter['date_month'],
				'subject'=>$val
			);
			$ls_val = $this->CurModel->get_totals($ls_arr);
			$ls_total = $ls_val * $bs;
			$arr['B'.$line] = $ls_total;
			
			
			foreach($xlist as $k=>$v){
				if($k<2){
					continue;
				}
				
				if(!isset($v['children'])){
					$ls_col = $this->get_char($k,'A');
					
					//$exp = isset($exps[$v['key']])?$exps[$v['key']]:0;
					//$arr[$ls_col.$line] =  toFloat($ls_total / $exp_total  * $exp);
					
					$arr[$ls_col.$line] = "=B{$line}/B4*{$ls_col}4";
				}
				else{
					
					foreach($v['children'] as $index=>$item){
						$ls_col = $this->get_char($k+$index,'A');
						
						//$exp = isset($exps[$item['key']])?$exps[$item['key']]:0;
						//$arr[$ls_col.$line] =  toFloat($ls_total / $exp_total  * $exp);
						
						$arr[$ls_col.$line] = "=B{$line}/B4*{$ls_col}4";
					}
				}
			}
			
			
			$total += $ls_total;
			$line++;
		}
		
		$arr['A'.$line] = '分摊费用';
		$arr['B'.$line] = $total;
		
		foreach($xlist as $k=>$v){
			if($k<2){
				continue;
			}
			
			if(!isset($v['children'])){
				$ls_col = $this->get_char($k,'A');
				
				//$exp = isset($exps[$v['key']])?$exps[$v['key']]:0;
				//$arr[$ls_col.$line] =  toFloat($total / $exp_total  * $exp);
				
				$arr[$ls_col.$line] = "=B{$line}/B4*{$ls_col}4";
			}
			else{
				
				foreach($v['children'] as $index=>$item){
					$ls_col = $this->get_char($k+$index,'A');
					
					//$exp = isset($exps[$item['key']])?$exps[$item['key']]:0;
					//$arr[$ls_col.$line] =  toFloat($total / $exp_total  * $exp);
					$arr[$ls_col.$line] = "=B{$line}/B4*{$ls_col}4";
				}
			}
		}
		
		
		
		$result = $this->Excel_write($arr,$merges);
		if(!$result){
			$this->err_out('生成Excel文件失败');
		}
		
		$url = base_url($result);
		
		$this->data_out($url);
	}
	

	
	function get_char($num,$pos='A'){
		if($num==0){
			return $pos;
		}

		for($i=0;$i<$num;$i++){
			$pos++;
		}
		return $pos;
	}
	
	function Excel_write($data,$merges=array()){
		$this->load->library('PHPExcel');
		
		// 创建Excel文件对象
		$objPHPExcel = new PHPExcel();
		// 设置文档信息，这个文档信息windows系统可以右键文件属性查看
		$objPHPExcel->getProperties()->setCreator("作者lalala")
			->setLastModifiedBy("最后更改者")
			->setTitle("文档标题")
			->setSubject("文档主题")
			->setDescription("文档的描述信息")
			->setKeywords("设置文档关键词")
			->setCategory("设置文档的分类");
		
		$objPHPExcel->setActiveSheetIndex(0);

		
									
		
		$objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('B3')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		foreach($data as $key=>$val){
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue($key,$val);
		}
		
		foreach($merges as $key=>$val){
			$objPHPExcel->setActiveSheetIndex(0)->mergeCells($val);
		}
		
		$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel ->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex(0))->setAutoSize(true); 
		//$objPHPExcel->getActiveSheet()->setTitle('第一个sheet');

		// 设置第一个sheet为工作的sheet
		

		// 保存Excel 2007格式文件，保存路径为当前路径，名字为export.xlsx
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$time = time();
		$dir  =  'upload/res/upload/other/'.date('Ymd');
		if(!is_dir(FCPATH . $dir)){
			@mkdir(FCPATH . $dir);
		}
		
		$file_name = $time.'_'.rand(10000,99999).'.xlsx';
		$objWriter->save(FCPATH . $dir .'/' .$file_name);
		
		return $dir .'/' .$file_name;
	}
	
	
	
}
